﻿namespace jzgl.Controller
{
    using jzgl.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Windows.Forms;

    internal class MaterTypeDB
    {
        private SqlCommand command = null;
        private SqlConnection conn = null;
        private ConnDB conndb = new ConnDB();
        private SqlDataReader reader = null;
        private DataTable table = null;
        private SysUser user = new SysUser();

        public MaterTypeDB()
        {
            this.conn = this.conndb.GetConnString();
        }

        public int Add_MaterType(MaterType mt)
        {
            int num = 0;
            try
            {
                try
                {
                    string sql = "insert into MaterType values('" + mt._MaterTypeName + "','" + mt._MaterTypeRemark + "')";
                    num = this.conndb.Execute_Sql(sql);
                }
                catch (Exception)
                {
                    throw;
                }
            }
            finally
            {
            }
            return num;
        }

        public int Count_MaterType()
        {
            int num = 0;
            try
            {
                try
                {
                    string sql = "select count(*) from MaterType";
                    num = this.conndb.Count_Record(sql);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
            return num;
        }

        public int Delete_MaterType()
        {
            int num = 0;
            try
            {
                try
                {
                    string sql = "delete from MaterType";
                    num = this.conndb.Execute_Sql(sql);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
            return num;
        }

        public int Delete_MaterType(MaterType mt)
        {
            int num = 0;
            try
            {
                try
                {
                    string sql = "delete from MaterType where MaterTypeId='" + mt._MaterTypeId + "'";
                    num = this.conndb.Execute_Sql(sql);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
            return num;
        }

        public int Show_MaterId_ByName(string type_name)
        {
            int num = 0;
            try
            {
                this.conn.Open();
                string cmdText = "select MaterTypeId from MaterType where MaterTypeName='" + type_name + "'";
                this.command = new SqlCommand(cmdText, this.conn);
                this.reader = this.command.ExecuteReader();
                if (this.reader.Read())
                {
                    num = this.reader.GetInt32(0);
                }
            }
            catch (Exception)
            {
            }
            finally
            {
                this.reader.Close();
                this.conn.Close();
            }
            return num;
        }

        public DataTable Show_MaterType()
        {
            try
            {
                try
                {
                    string sql = "select * from MaterType";
                    this.table = this.conndb.FillDataTable(sql);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
            return this.table;
        }

        public void Show_MaterType(ComboBox box)
        {
            try
            {
                try
                {
                    string sqlstr = "select * from MaterType";
                    this.conndb.comboxDataBind(box, sqlstr, "MaterTypeId", "MaterTypeName");
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
        }

        public void Show_MaterType(DataGridView dgv)
        {
            try
            {
                try
                {
                    string sqlstr = "select * from MaterType";
                    this.conndb.dgvDataBind(dgv, sqlstr);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
        }

        public int Update_MaterType(MaterType mt)
        {
            int num = 0;
            try
            {
                try
                {
                    string sql = string.Concat(new object[] { "update MaterType set MaterTypeName='", mt._MaterTypeName, "',MaterTypeRemark='", mt._MaterTypeRemark, "'  where  MaterTypeId='", mt._MaterTypeId, "'" });
                    num = this.conndb.Execute_Sql(sql);
                }
                catch (Exception)
                {
                }
            }
            finally
            {
            }
            return num;
        }
    }
}

